package com.example.recyclerview_206shj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;//RecyclerView控件
    private RecyclerView.LayoutManager layoutManager;//RecyclerView控件布局管理
    private MyAdapter myAdapter;//RecyclerView的适配器

    private List<ArticleBean> list = null;//保存数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    //初始化RecyclerView
    private void init(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //创建默认的线性布局
        layoutManager = new LinearLayoutManager(this);
        //设置线性布局
        recyclerView.setLayoutManager(layoutManager);
        //设置每个item固定高度，提高刷新性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        list = getData();
        //实例化Adapter
        myAdapter = new MyAdapter(this);
        myAdapter.setList(list);
        //绑定recyclerview和adapter
        recyclerView.setAdapter(myAdapter);
    }

    //获取数据源并返回List
    private List<ArticleBean> getData(){
        List<ArticleBean> list = new ArrayList<ArticleBean>();
        ArticleBean articleBean;
        //第一组数据
        articleBean = new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("title1");
        articleBean.setDescription("desp1");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);

        //第二组数据
        articleBean = new ArticleBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("title2");
        articleBean.setDescription("desp2");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);

        //第三组数据
        articleBean = new ArticleBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("title3");
        articleBean.setDescription("desp3");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);

        //第四组数据
        articleBean = new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("title4");
        articleBean.setDescription("desp4");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);

        //第五组数据
        articleBean = new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("title5");
        articleBean.setDescription("desp5");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);

        //第六组数据
        articleBean = new ArticleBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("title6");
        articleBean.setDescription("desp6");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);

        return list;
    }

}
